package com.example.demo.service.impl;

import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.ICollectionMapper;
import com.example.demo.param.vo.CollectionVO;
import com.example.demo.param.vo.FollowUserVO;
import com.example.demo.service.ICollectionService;
import com.example.demo.util.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CollectionServiceImpl implements ICollectionService {
    @Autowired private ICollectionMapper collectionMapper;

    @Override
    public Boolean insert(String userId, String questionId) throws ServiceException {
        if (Judge.isNull(questionId)){
            throw new ServiceException("请务必完善必填项!");
        }
        Integer affect = this.collectionMapper.insert(Judge.getUUID(),userId,questionId,0,new Date());
        if (affect != 1){
            throw new ServiceException("收藏失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(String id) throws ServiceException {
        if (Judge.isNull(id)){
            throw new ServiceException("请务必完善必填项!");
        }
        Integer affect = this.collectionMapper.delete(id);
        if (affect != 1){
            throw new ServiceException("取消收藏失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public CollectionVO getMyCollection(String userId, Integer page, Integer pageSize) {
        if(page == null || page < 1 || pageSize == null || pageSize < 1){
            throw new ServiceException("请提供正确的分页");
        }
        CollectionVO vo = this.collectionMapper.getMyCollection(userId,page,pageSize);
        vo.setPage(page);
        vo.setPageSize(pageSize);
        return vo;
    }
}
